﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderAggregator.DataStorage;

namespace OrderAggregator.UI
{
    public partial class OrganizationListControl : BaseEditListControl
    {
        private DataStorage.Storage.OrganizationDataTable _organizationDataTable = null;
        
        public OrganizationListControl()
        {
            InitializeComponent();            
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        
        /// <summary>
        /// Init
        /// </summary>
        /// <param name="organizationDataTable"></param>
        public void Init(DataStorage.Storage.OrganizationDataTable organizationDataTable)
        {
            _organizationDataTable = organizationDataTable;

            organizationTreeList1.SelectedOrganizationChanged -= organizationTreeList1_SelectedOrganizationChanged;

            organizationTreeList1.Init(organizationDataTable);

            organizationTreeList1.SelectedOrganizationChanged += organizationTreeList1_SelectedOrganizationChanged;

            organizationControl1.Init(organizationDataTable);            
        }     
       
        private void organizationTreeList1_SelectedOrganizationChanged(object sender, EventArgs e)
        {
            organizationControl1.Organization = organizationTreeList1.SelectedOrganization;           
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            var parentOrg = organizationTreeList1.SelectedOrganization;

            var newRow = _organizationDataTable.NewOrganizationRow();
            newRow.OrganizationRowParent = parentOrg;
            newRow.Name = "";
            newRow.Addres = "";
            _organizationDataTable.AddOrganizationRow(newRow);
            organizationTreeList1.SelectedOrganization = newRow;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var organizationRow = organizationTreeList1.SelectedOrganization;
            organizationRow.Delete();
        }      
    }
}
